# test scripts
use PlPN qw(PN);

=pod

To test regular script execution, simply select the test script from the scripts window

To test doc as script, open 'test_doc_as_script.pl' and select 'Use As Script' from the context menu. This will put it in the scripts window; select it from there.

To test eval, create a tool and anywhere you would use one of the shortcuts shown under 'Special Symbols', use '&{runnerId:code}', and Eval(code) will be called in the runner with the given id.

I have found three instances in the PN code where Exec is called, all of which use the builtin flag. Under the default version of Exec(), the builtin flag means the function will be called as a method on the PlPN class; otherwise, it's called as a method on the runner object. The capture output flag means that Exec() will return whatever the function prints without a filehandle, rather than the return value of the function. To test Exec, create a runner with the id 'python' (this id is hard-coded in PN itself).

Note: you can override PlPN class methods by creating a descendant class and writing your own versions of the methods. Then set the variable C<$PlPN::PlPN> to your package name or to an object blessed into the class. You can still print to STDOUT by explicitly naming it as the filehandle when your print; the capture mechanism works by C<select>ing a filehandle tied to a scalar.

Changing the text in the command bar will call Exec('evalCommand', $param, efBuiltIn), where $param is the text from the command bar.

Hitting enter in the command bar will call Exec('evalCommandEnter', $param, efBuiltIn), where $param is the text from the command bar.

Using backticks in a clip description will call Exec('evalScript', $param, efBuiltIn|efCaptureOutput), where $param is the text inside the the backticks.

=cut

my $registry = PN->GetScriptRegistry; 

my $runner = new PlPN::ScriptRunner;

$registry->RegisterRunner('test', $runner);

$registry->EnableSchemeScripts('perl', 'test');

$runner->{'scripts'}{'test'} = sub {
	PlPN->alert("Test Script successfully called");
};

$registry->Add('test', 'test', 'test:test');

#	$registry->RemoveRunner('perl');

#	my $result = $registry->GetRunner('perl');

#
# to test Exec
#

{
	package FakePythonRunner;
	our @ISA = 'PlPN::ScriptRunner';
	
	package PlPN;
	
	# called when text in command bar changes
	sub evalCommand {
		my ($self, $param) = @_;
PlPN->alert($param);
		return "evalCommand";
	}
	
	# called when enter is hit in command bar
	sub evalCommandEnter {
		my ($self, $param) = @_;
PlPN->alert($param);
		return "evalCommandEnter";
	}
	
	# called via backticks in clips; called with the capture output flag set
	sub evalScript {
		my ($self, $param) = @_;
		$param=~s/\r\n?/\n/g;
		my $ret = eval $param;
PlPN->alert(qq("$param" => "$ret"));
		print $ret;
		return $ret;
	}
}

my $fake_python = new FakePythonRunner;

$registry->RegisterRunner('python', $fake_python);

